<script setup lang="ts">
import { ref } from "vue";
import { useOrders } from "./utils/hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import {hasPerms} from "@/utils/auth";
import { useI18n } from "vue-i18n";
const { t } = useI18n();
import { useRoute } from 'vue-router'
const route = useRoute()
import Refresh from "@iconify-icons/ep/refresh"
defineOptions({
  name: "PendingCustomers"
});
import { Location } from '@element-plus/icons-vue'
const formRef = ref();
const tableRef = ref();
const {
  form,expandedRowKeys, loading, columns,stateList,importData,
  dataList,salesman_arr ,onSaveCrmBatch,copy_customer,
  shopList,setBatchSearch,searchCondList,
  sortChange,selectedNum,onSelectionCancel,expandStatus,
  onSearch,export_data,statistics,tabStats,
  resetForm, openDialog,
  handleDelete, pagination, handleSizeChange,  handleCurrentChange, handleSelectionChange
} = useOrders(tableRef);
const locationVisible = ref<boolean>(false)

function onFullscreen() { // 重置表格高度
  tableRef.value.setAdaptive();
}
const size = ref("small");
</script>

<template>
  <div class="main">
    <el-form ref="formRef" :inline="true" :size="size" :model="form" class="search-form bg-bg_color w-[99/100] pl-4 pt-[12px] overflow-auto" >
      <el-form-item label="" prop="own_saleman_id">
        <el-select v-model="form.own_saleman_id" placeholder="归属业务员" clearable filterable class="!w-[100px]">
          <el-option v-for="(item) in salesman_arr" :label="(item?.site_group[0]>0?(item?.sales_group):(item.dept?.name))+'-'+item.username" :value="item.id" :disabled="item.id==''"/>
        </el-select>
      </el-form-item>

      <el-form-item label="" prop="poll_saleman_id">
        <el-select v-model="form.poll_saleman_id" placeholder="轮询业务员" clearable filterable class="!w-[100px]">
          <el-option v-for="(item) in salesman_arr" :label="(item?.site_group[0]>0?(item?.sales_group):(item.dept?.name))+'-'+item.username" :value="item.id" :disabled="item.id==''"/>
        </el-select>
      </el-form-item>

      <el-form-item label="" prop="gender">
        <el-select v-model="form.gender"  placeholder="性别"  clearable class="!w-[88px]">
          <el-option label="不限" value="0" />
          <el-option label="男" value="1" />
          <el-option label="女" value="2" />
        </el-select>
      </el-form-item>

      <el-form-item prop="search">
        <el-input v-model="form.search" style="max-width: 600px; " clearable autosize @dblclick="setBatchSearch" placeholder="可批量查询" class="input-with-select" >
          <template #prepend>
            <el-select v-model="form.search_field" placeholder="选择查询条件"  style="width: 100px">
              <el-option v-for="(item) in searchCondList" :label="'按'+item.name" :value="item.value" :disabled="item.value==''"/>
            </el-select>
          </template>
        </el-input>
      </el-form-item>

      <el-form-item prop="time">
        <el-select v-model="form.search_field2" placeholder="选择时间" class="search_field" style="width: 100px;border-radius:0">
          <el-option label="释放到公共池子时间" value="release_time" />
          <el-option label="首次付款时间" value="frist_pay_at" />
          <el-option label="最近访问时间" value="last_visit_time" />
          <el-option label="最后付款时间" value="last_pay_at" />
          <el-option label="轮询分配时间" value="poll_time" />
          <el-option label="加入时间" value="created_at" />
          <el-option label="最后修改时间" value="updated_at" />
        </el-select>
        <el-date-picker
          v-model="form.time" value-format="YYYY-MM-DD" format="YYYY-MM-DD"
          type="daterange" style="width: 200px;border-radius: 0px;border-left: 0;margin-left: -1px;"
          range-separator="~" prefix-icon=""
          start-placeholder="Start date"
          end-placeholder="End date"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" :icon="useRenderIcon('ri:search-line')" :loading="loading" @click="onSearch"> {{ t("buttons.search") }}</el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)" style="margin-left:5px;">{{ t("buttons.reset") }}</el-button>
      </el-form-item>
    </el-form>

    <PureTableBar :title="t(route.meta.title)"  :isExpandAll="expandStatus"
      :columns="columns" :tableRef="tableRef?.getTableRef()"
      @refresh="onSearch" @fullscreen="onFullscreen">
      <template #title>
        <el-tabs v-model="form.tab_id" class="bg-bg_color  w-[99/100]  pt-[1px] overflow-auto" style="margin-top:-8px;" @tab-click="onSearch">
          <el-tab-pane :label="`Top待轮询(${tabStats.tab_1_count})`" name="1"></el-tab-pane>
          <el-tab-pane :label="`弃单待轮询(${tabStats.tab_2_count})`" name="2"></el-tab-pane>
          <el-tab-pane :label="`待处理待轮询(${tabStats.tab_3_count})`" name="3"></el-tab-pane>
          <div >
            <span v-if="selectedNum > 0" style="font-size: var(--el-font-size-base)" class="text-[rgba(42,46,54,0.5)] dark:text-[rgba(220,220,242,0.5)]">
              已选 {{ selectedNum }} 项
            </span>

            <el-button v-if="selectedNum>0" @click="copy_customer('email')" :size="size">{{ t("buttons.copyOrderEmail") }}</el-button>
            <el-button v-if="dataList.filter(x=>x.isModified==true).length>0" @click="onSaveCrmBatch" :size="size">一键保存</el-button>

          </div>
        </el-tabs>
      </template>

      <template v-slot="{ size, dynamicColumns }">
        <pure-table ref="tableRef"  adaptive  @sort-change="sortChange"
          :adaptiveConfig="{ offsetBottom: 90 }" :expand-row-keys="expandedRowKeys"
                    align-whole="center" row-key="id" showOverflowTooltip table-layout="auto" cell-class-name="copyable-cell"
          :loading="loading" :size="size" :data="dataList"
          :pagination="{ ...pagination, size }" :columns="dynamicColumns"
          :header-cell-style="{ background: 'var(--el-fill-color-light)',color: 'var(--el-text-color-primary)' }"
          :border="true" @selection-change="handleSelectionChange"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        >
        </pure-table>
      </template>
    </PureTableBar>
  </div>
</template>

<style lang="scss" scoped>
:deep(.el-table__inner-wrapper::before) { height: 0;}
:deep(.search_field .el-select__wrapper) { border-radius: 0;border-right: 0;margin-right: -1px;}
:deep(.el-form--inline .el-form-item) {margin-right: 8px;}
.main-content {margin: 18px 18px 0 !important;}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 8px;
  }
}
</style>
